﻿using SHomeWorkshop.LunarMind.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SHomeWorkshop.LunarMind.LMETools
{
    public class DemoResourceButton : ToggleButton
    {
        public DemoResourceButton(GraphicDemonstrator demoWin, DemoResourceInfo resourceInfo, Canvas mainCanvas)
        {
            this.Background = Brushes.Transparent;
            this.ResourceInfo = resourceInfo;
            this.ResourceCard = new DemoResourceCard(demoWin, this)
            {
                Visibility = System.Windows.Visibility.Collapsed,
                Title = resourceInfo.LinkTitle,
                WindowStartupLocation = System.Windows.WindowStartupLocation.Manual,
                ShowInTaskbar = false,
            };

            spBase.Children.Add(imgType);
            spBase.Children.Add(tbTitle);

            if (resourceInfo.LinkType == "Html")
            {
                imgType.Source = htmlTag;
            }
            else  // Image
            {
                imgType.Source = pictureTag;
            }

            tbTitle.Text = resourceInfo.LinkTitle;

            this.Content = spBase;

            this.mainCanvas = mainCanvas;

            this.Click += DemoResourceButton_Click;
        }

        static DemoResourceButton()
        {
            htmlTag = new BitmapImage(new Uri("pack://application:,,,/LunarMind;component/Resources/htmltag.png"));
            pictureTag = new BitmapImage(new Uri("pack://application:,,,/LunarMind;component/Resources/picturetag.png"));
        }

        private static BitmapImage htmlTag;

        private static BitmapImage pictureTag;

        private StackPanel spBase = new StackPanel() { Orientation = Orientation.Horizontal, };

        private Image imgType = new Image() { VerticalAlignment = System.Windows.VerticalAlignment.Center, };

        private TextBlock tbTitle = new TextBlock() { VerticalAlignment = System.Windows.VerticalAlignment.Center, };

        /// <summary>
        /// 窗口应在此 Canvas 中显示。须传入。
        /// </summary>
        private Canvas mainCanvas;

        private void DemoResourceButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ResourceCard.Visibility == System.Windows.Visibility.Visible)
            {
                HideCard();
            }
            else
            {
                ShowCard();
            }

            SwitchLayoutPanels();
        }

        public void SwitchLayoutPanels()
        {
            var win = System.Windows.Window.GetWindow(this);
            if (win == null) return;

            if (win is GraphicDemonstrator gwin)
            {
                var openedCardsCount = 0;
                foreach (var element in gwin.UgResources.Children)
                {
                    if (element is DemoResourceButton dbtn)
                    {
                        if (dbtn.ResourceCard.Visibility == System.Windows.Visibility.Visible)
                        {
                            openedCardsCount++;
                        }
                    }
                }

                gwin.sp1.Visibility = gwin.sp2.Visibility = gwin.sp3.Visibility = gwin.sp4.Visibility = gwin.sp5.Visibility = gwin.sp6.Visibility = System.Windows.Visibility.Collapsed;
                switch (openedCardsCount)
                {
                    case 1: gwin.sp1.Visibility = System.Windows.Visibility.Visible; break;
                    case 2: gwin.sp2.Visibility = System.Windows.Visibility.Visible; break;
                    case 3: gwin.sp3.Visibility = System.Windows.Visibility.Visible; break;
                    case 4: gwin.sp4.Visibility = System.Windows.Visibility.Visible; break;
                    case 5: gwin.sp5.Visibility = System.Windows.Visibility.Visible; break;
                    case 6: gwin.sp6.Visibility = System.Windows.Visibility.Visible; break;
                }
            }
        }

        public void ShowCard()
        {
            var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
            var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.Width, this.mainCanvas.Height));

            this.ResourceCard.Left = leftTop.X;
            this.ResourceCard.Top = leftTop.Y;
            this.ResourceCard.Width = this.mainCanvas.ActualWidth;
            this.ResourceCard.Height = this.mainCanvas.ActualHeight;
            this.ResourceCard.Show();

            this.IsChecked = true;
        }

        public void HideCard()
        {
            this.ResourceCard.Close();

            this.IsChecked = false;
        }

        public DemoResourceInfo ResourceInfo { get; }

        public DemoResourceCard ResourceCard { get; }
    }
}
